package tag36_29_08.regExWieder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reg05_AufgabePhilosophen {

	public static void main(String[] args) throws Exception  {

//		String philosophen = philosophenAll(dateiLesen("Liste_antiker_Philosophen.txt"), "<li><.*?</li>");

//		/** Philosophen Namen */
//		System.out.println("*************************  NAMEN  ********************************");
//		String namen = philosophenAll(philosophen,"\">.*?<");
//		
//		
//		System.out.println(namen);
//		String[]  cadenas = namen.split("[\"><]\\s");
//		System.out.println(cadenas.length);
////		int count=1;
////		for (String string : getNamen(namen)) {
////			System.out.println(count++ +". "+string);
////		}
//		
//		/** Philosophen Geburtsjahr */
//		System.out.println("\n*************************  GEBURTSJAHR  ***************************");
//		String geburtsJahr = philosophenAll(philosophen, "a>.*</li");
//		System.out.println(geburtsJahr);
//		System.out.println(getProperty(geburtsJahr, "\\(\\d+| \\d+|a></li"));
		
		
//		
//		String geburt = philosophenAll(geburtsJahr, "\\(\\d+| \\d+|a></li");
//		System.out.println(geburt);
//		suchen(geburt,"\\d+");
//		String[]  cadena = geburt.split("[d+]");
//		
//		for (String string : cadena) {
//			System.out.println(string);
//		}
//		System.out.println(cadena.length);
//		
		
		
//		suchen(geburtsJahr, "\\(\\d+");
//		suchen(geburtsJahr, "\\(\\d+| \\d+|a></li");
//		String geburt = philosophenAll(geburtsJahr, "\\(\\d+| \\d+|a></li");
//		System.out.println(geburt);
		
//		System.out.println(geburt);
//		String[]  cadenas = geburt.split("\\d+");
//		System.out.println(cadenas.length);
//		for (int i = 0; i < cadenas.length; i++) {
//			System.out.println(cadenas[i].substring(1));
//		}
		
		
//		
//		int count1=1;
//		for (String string : getGeburtjahr(geburtsJahr)) {
//			System.out.println(count1++ +". "+string);
//		}

		
		
//		System.out.println("--------------Philosophenamen------------------------");
//		int count=1;
//		for (String string : getPerson(texto)) {
//			System.out.println(count++  +".  "+ string);
//		}
		
	} // End of Main
	
	static String getProperty(String row, String reg) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(row);
		m.find();
		return m.group().replace("(", "").replace(" ", "").replace("a></li", "");
	}

	
	static String getPerson(String txt){
//		List<String> listName = new ArrayList<String>();
		Pattern p = Pattern.compile("\">.*?</li>");
		Matcher m = p.matcher(txt);
		String name="";   
		while (m.find()){
			name += m.group()+"\n";
		}
//		listName = philoName(name);	
		return name;
	}
	
	
	/*
	static List<String> getNamen(String txt){
		List<String> listName = new ArrayList<String>();
		Pattern p = Pattern.compile("\">.*?<");
		Matcher m = p.matcher(txt);
		String name="";   
		while (m.find()){
			name += m.group()+"\n";
		}
		listName = philoName(name);	
		return listName;
	}
	*/
//	static List<String> getGeburtjahr(String txt){
//		List<String> listPhil = new ArrayList<String>();
//		String geburt = philosophenAll(txt, "\\(\\d+| \\d+|a></li");
//		String[]  cadenas = geburt.split("[d+]");
//	    for(int i = 0; i<cadenas.length; i++){
//	    	if(cadenas[i].equals("a></li")){
//	    		cadenas[i]=" ";
//	    		listPhil.add(cadenas[i]);
//	    	}else{	
//	    		listPhil.add(cadenas[i]);
//	    	}
//	    }
//	    listPhil = listPhil.subList(0, listPhil.size()); 
//		return listPhil;
//	}
	
	static List<String> getNamen(String txt){
		List<String> listPhil = new ArrayList<String>();
		String[]  cadenas = txt.split("[\"><]\\s");
	    for(int i = 0; i<cadenas.length; i++){
	    	if(i!=32){
	    		listPhil.add(cadenas[i].substring(2));
	    	}
	    }
	    listPhil = listPhil.subList(0, listPhil.size()-7); 
		return listPhil;
	}		
	
	
	static List<String> philosophenAll(String txt, String reg){
		List<String> cont = new ArrayList<String>();
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(txt);
		
		while (m.find()){
			cont.add(m.group());
		}
		
		cont = cont.subList(0, cont.size()-7);
		return cont;
	}		
	
	
	static String dateiLesen(String txt) throws Exception{
		String sCadena = null, texto = new String();
		BufferedReader bf = new BufferedReader(new FileReader(txt));
		while ((sCadena = bf.readLine())!=null) {
			    texto += sCadena + "\n";
			}
		bf.close();
		return texto;
	}
	
	/**
	 * AYUDAS
	 * */
	
	static void suchen(String in, String reg){
		System.out.println("----------------------------------------");
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(in);
		int count =1;
		while (m.find()){
			System.out.println(count++ +". Pos: "+ m.start() +" bis: "+m.end()+ " - Gefunden: " + m.group());
		}

	}
	
	static void urlDonwload(String urls) throws Exception{
		URL url = new URL(urls);
		InputStream is = url.openStream();
		InputStreamReader reader = new InputStreamReader(is);
		BufferedReader in = new BufferedReader(reader);
		
		String line;
		
		while((line = in.readLine()) != null){
			System.out.println(line);
		}
		in.close();
	}

}
